package com.qzy.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.mapper.DishMapper;
import com.qzy.reggie.pojo.Dish;
import com.qzy.reggie.service.DishService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult queryList(Integer page, Integer pageSize, String name) {
        // 使用Mybatis的PageHelper插件完成分页功能

        // 1.通过插件，设置分页信息
        PageHelper.startPage(page,pageSize);

        // 2.正常实现员工列表查询（此时不需要考虑分页）
        List<Dish> Dishs = dishMapper.selectList(name);

        // 3.封装PageInfo对象，获取分页total
        PageInfo<Dish> pageInfo = new PageInfo<>(Dishs);

        // 最后封装分页结果对象
        return new PageResult(pageInfo.getTotal(),Dishs);
    }

    @Override
    public Dish queryById(Long id) {
        if(id == null){
            return null;
        }
        Dish dish = new Dish();
        dish.setId(id);

        return dishMapper.selectOne(dish);
    }

    @Override
    public Integer save(Dish dish) {
        dish.setSort(10);
        dish.setCode(String.valueOf(RandomUtils.nextInt(100000,10000000)));
        return dishMapper.insert(dish);
    }

    @Override
    public Integer update(Dish dish) {
        return dishMapper.update(dish);
    }

    @Override
    public Integer deleteById(Long id) {
        return dishMapper.delete(id);
    }

}
